<template>
  <div>
    <el-button @click="onSubmit(1)">刷新出入信息 <i class="el-icon-circle-plus-outline"></i></el-button>
    <el-button type="text" @click="dialogFormVisible = true">添加出入信息</el-button>
    <el-dialog title="更新信息" :visible.sync="dialogFormVisible">
      <el-form :model="handlefrom" :rules="rules" ref="rulesForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="车辆信息">
          <el-select v-model="handlefrom.carId" placeholder="请选择车辆">
            <el-option v-for="handle in list" :key="handle.value" :label="handle.plateNum" :value="handle.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注"  prop="phone">
          <el-input v-model="handlefrom.remark" autocomplete="off" ></el-input>
        </el-form-item>
        是否离开
        <el-radio v-model="handlefrom.go" :label="1">否</el-radio>
        <el-radio v-model="handlefrom.go" :label="0">是</el-radio>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" type="primary" >取 消</el-button>
        <el-button @click="submit" type="primary" >确 定</el-button>
      </div>
    </el-dialog>

    <el-table
        :data="pageInfo.list" v-loading="loading"
        style="width: 100%"
        :row-class-name="tableRowClassName">
      <el-table-column label="出入（进入，离开）">
        <template #default="scope">
          <span v-if="scope.row.tempCar">离开</span>
          <span v-else>进入</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="carId"
          label="车辆id"
      ></el-table-column>
      <el-table-column
          prop="time"
          label="出入时间"
          :formatter="formatDate"
      ></el-table-column>
      <el-table-column
          prop="remark"
          label="备注">
      </el-table-column>
      <!-- 逻辑删除 -->
      <el-table-column
          fixed="right"
          label="操作"
          width="100">
        <template slot-scope="scope">
          <el-button @click="delect(scope.$index,scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="pageNum"
        background
        layout="prev, pager, next"
        @current-change="handlePageNum"
        :current-page="pageInfo.pageNum"
        :page-size="pageInfo.pageSize"
        :total="pageInfo.total"> <!-- 确保绑定到正确的总记录数 -->
    </el-pagination>
  </div>
</template>
<script>
import {list} from "postcss";

export default {
  name: "AccessRecord",
  data(){
    return {
      list:[],
      pageInfo:[],
      dialogFormVisible:false,
      handlefrom:{
        go:'',
        remark:'',
        time:'',
        carId:'',
        region:'',
      },
      form: {
        id:'',
        plateNum: '',
        color: '',
        brand:'',
        startTime:'',
        endTime:'',
        phone: '',
        type: '',
        tempCar: '',
        leave:'',
        remark:'',
        updateTime:'',
        region:'',
      },
      loading:false,
    }
  },
  created() {
    this.onSubmit(1)
  },
  methods:{
    delect(index,row){
      this.axios.delete("Access/"+row.id).then(res=>{
        this.onSubmit(1)
      })
    },
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }
      return '';
    },
    formatDate(row, column, cellValue) {
      if (!cellValue) return '';
      const date = new Date(cellValue);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    submit(){
      let params={id:this.form.region}
      // console.log(params)
      this.axios.post("Access/handleinsert",this.handlefrom).then(res=>{
        this.dialogFormVisible = false;
        this.handlefrom={}
      })
    },
    onSubmit(pageNum){
      this.loading = true;
      this.axios.post("Access/add",this.handlefrom,{params:{pageNum:pageNum}}).then((res)=>{
        this.pageInfo = res.data.pageInfo;
        this.loading = false;
      })
      this.axios.get("Access/handlelist").then(res=>{
        this.list=res.data.handlelist;
      })
    },
    handlePageNum(pageNum){
      this.onSubmit(pageNum);
    }
  }
}

</script>
<style>
.pageNum{
  display: flex;
  justify-content: center; /* 水平居中 */
  padding-top: 30px;
}
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}

</style>